package com.thinkfit.jy.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import com.common.lib.bean.VersionBean;
import com.common.lib.constant.EventBusEvent;
import com.thinkfit.jy.R;
import com.thinkfit.jy.fragment.WifiConnectFragment;
import com.thinkfit.jy.serialport.SerialPortManager;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 快捷wifi界面
 */
public class SimpleWifiViewUtils  extends PopupWindow implements View.OnClickListener {

    private static SimpleWifiViewUtils simpleWifiViewUtils;
    WindowManager windowManager;
    Context mContext;
    FragmentActivity mActivity;
    boolean isFist = true;
    View inflate;
    ConstraintLayout clBg;
    FrameLayout flWifi;

    public static SimpleWifiViewUtils getInstance(FragmentActivity activity){
        if(simpleWifiViewUtils == null){
            simpleWifiViewUtils = new SimpleWifiViewUtils(activity);
        }
        return simpleWifiViewUtils;
    }

    public SimpleWifiViewUtils(FragmentActivity activity){
        mActivity = activity;
        inflate = LayoutInflater.from(activity).inflate(R.layout.item_simple_wifi, null);
        clBg = inflate.findViewById(R.id.cl_bg);
        flWifi = inflate.findViewById(R.id.fl_wifi);

        clBg.setOnClickListener(this);

        this.setContentView(inflate);
        WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();

        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(false);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.parseColor("#4D000000"));
//        ColorDrawable dw = new ColorDrawable(Color.parseColor("#ffffff"));
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
//        this.setOnDismissListener(this);

        toFragment(new WifiConnectFragment());

    }

    public void show() {
        showAtLocation(mActivity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }

//    public void initView(Context context, FragmentActivity activity){
//        mContext = context;
//        mActivity = activity;
//        if(windowManager == null){
//            windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
//        }
//        if(inflate == null){
//            inflate = LayoutInflater.from(context).inflate(R.layout.item_simple_wifi, null);
//            clBg = inflate.findViewById(R.id.cl_bg);
//            flWifi = inflate.findViewById(R.id.fl_wifi);
//        }
//
//        clBg.setOnClickListener(this);
//
//
//
//        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
//                WindowManager.LayoutParams.MATCH_PARENT,
//                WindowManager.LayoutParams.MATCH_PARENT,
//                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
//                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
//                PixelFormat.TRANSLUCENT
//        );
//
//        layoutParams.gravity = Gravity.CENTER;
//
//        if(isFist){
//            isFist = false;
//            windowManager.addView(inflate,layoutParams);
//        }else {
//            inflate.setVisibility(View.VISIBLE);
//        }
//
//        toFragment(new WifiConnectFragment());
//
//    }

    public void toFragment(Fragment fragment) {
        FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
//        ft.setCustomAnimations(R.anim.slide_fragment_in,R.anim.slide_fragment_out);
        ft.replace(R.id.fl_wifi, fragment);
        ft.commit();
    }


    public void goneLayout(){
        if(windowManager != null){
            if(inflate != null){
                inflate.setVisibility(View.GONE);
            }
        }

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.cl_bg:
                dismiss();
//                goneLayout();
                break;
        }
    }
}
